# -*- coding: utf-8 -*-

'''
基于webhook的自动部署脚本

目前在python27+git@OSC下测试通过

'''

import sys,os
import json
import traceback
import logging  
import logging.handlers
from win32com.client import Dispatch, constants, gencache


__author__ = 'Luderson'

cfg_repo_path = 'C:/git' # 
LOG_FILE = 'log.log'  # 日志

#初始化日志
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 实例化handler   
fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s'  
formatter = logging.Formatter(fmt)   # 实例化formatter  
handler.setFormatter(formatter)      # 为handler添加formatter  
logger = logging.getLogger('tst')    # 获取名为tst的logger  
logger.addHandler(handler)           # 为logger添加handler  
logger.setLevel(logging.DEBUG)  
# 之后便可以这样来用：
#logger.info('first info message')  
#logger.debug('first debug message') 

logger.debug('first debug message') 
  
def doc2pdf(input, output):  
  w = Dispatch("Word.Application")  
  
  try:  
    doc = w.Documents.Open(input, ReadOnly = 1)  
    doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,   
      Item = constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks)  
    return 0  
  except:  
    return 1  
  finally:  
    w.Quit(constants.wdDoNotSaveChanges)  
  
# Generate all the support we can.  
def GenerateSupport():  
  # enable python COM support for Word 2007  
  # this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library"  
  gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)  
  
def main():  
  if (len(sys.argv) == 2):  
    input = sys.argv[1]  
    output = os.path.splitext(input)[0]+'.pdf'  
  elif (len(sys.argv) == 3):  
    input = sys.argv[1]  
    output = sys.argv[2]  
  else:  
    input = u'BA06007013.docx'#word文档的名称  
    output = u'BA06007013.pdf'#pdf文档的名称  
  if (not os.path.isabs(input)):  
    input = os.path.abspath(input)  
  if (not os.path.isabs(output)):  
    output = os.path.abspath(output)  
  try:  
    GenerateSupport()  
    rc = doc2pdf(input, output)  
    return rc  
  except:  
    return -1  
  
if __name__=='__main__':  
    rc = main()  
    if rc:  
        sys.exit(rc)  
    sys.exit(0)  








